package com.inforich.web.controller.area;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.inforich.building.domain.BuildingType;
import java.util.Arrays;
import cn.hutool.core.bean.BeanUtil;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import com.inforich.common.annotation.Log;
import com.inforich.common.core.controller.BaseController;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.BusinessType;
import com.inforich.area.domain.Area;
import com.inforich.area.service.IAreaService;
import com.inforich.common.utils.poi.ExcelUtil;

/**
 * 地区Controller
 *
 * @author hjm
 * @date 2022-10-23
 */
@RestController
@RequestMapping("/area/area")
public class AreaController extends BaseController {

  @Autowired
  private IAreaService areaService;

  /**
   * 查询地区列表
   */
  @PreAuthorize("@ss.hasPermi('area:area:list')")
  @GetMapping("/list")
  public Page<Area> list(Page<Area> page, Area area) {
    Map<String, Object> map = beanToMap(area);
    areaService.pageArea(page, map);
    return page;
  }


  /**
   * 导出地区列表
   */
  @PreAuthorize("@ss.hasPermi('area:area:export')")
  @Log(title = "地区", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, Area area) {
    Map<String, Object> map = beanToMap(area);
    List<Area> list = areaService.listArea(map);
    ExcelUtil<Area> util = new ExcelUtil<Area>(Area.class);
    util.exportExcel(response, list, "地区数据");
  }

  /**
   * 获取地区详细信息
   */
  @PreAuthorize("@ss.hasPermi('area:area:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") String id) {
    return AjaxResult.success(areaService.getById(id));
  }

  /**
   * 新增地区
   */
  @PreAuthorize("@ss.hasPermi('area:area:add')")
  @Log(title = "地区", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated @RequestBody Area area) {
    return toAjax(areaService.save(area));
  }

  /**
   * 修改地区
   */
  @PreAuthorize("@ss.hasPermi('area:area:edit')")
  @Log(title = "地区", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@Validated @RequestBody Area area) {
    return toAjax(areaService.updateById(area));
  }

  /**
   * 删除地区
   */
  @PreAuthorize("@ss.hasPermi('area:area:remove')")
  @Log(title = "地区", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public AjaxResult remove(@PathVariable String[] ids) {
    return toAjax(areaService.removeByIds(Arrays.asList(ids)));
  }

  /**
   * 地区树
   */
  @GetMapping("/tree")
  public AjaxResult selectBuildingTypeTreeList() {
    List<Area> parentList = areaService.list(new LambdaQueryWrapper<Area>()
        .eq(Area::getLevel, "10"));
    List<Area> list1 = areaService.list(new LambdaQueryWrapper<Area>()
        .eq(Area::getLevel, "20"));
    List<Area> list2 = areaService.list(new LambdaQueryWrapper<Area>()
        .eq(Area::getLevel, "30"));
    list2.addAll(list1);
    return AjaxResult.success(areaService.selectAreaTreeList(parentList, list2));
  }
}
